package com.example.roomdemo

import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.LogUtils
import com.example.roomdemo.bean.User
import com.example.roomdemo.db.RoomUtils
import kotlinx.coroutines.*

class MainActivity : AppCompatActivity() {
    private lateinit var roomUtils: RoomUtils
    private val tvAdd:TextView by lazy { findViewById(R.id.tv_add) }
    private val tvUpdate:TextView by lazy { findViewById(R.id.tv_update) }
    private val tvDelete:TextView by lazy { findViewById(R.id.tv_delete) }
    private val tvQuery:TextView by lazy { findViewById(R.id.tv_query) }
    private val tvDeleteUsers:TextView by lazy { findViewById(R.id.tv_delete_users) }
    private val tvDeleteAll:TextView by lazy { findViewById(R.id.tv_delete_all) }
    private val TAG: String = "MainLogUtils"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initDataBase()
        initView()
    }

    private fun initDataBase() {
        roomUtils = RoomUtils.getInstance();
    }

    @OptIn(DelicateCoroutinesApi::class)
    private fun initView() {
        tvAdd.setOnClickListener {
            GlobalScope.launch(Dispatchers.Main) {
                // 插入一条用户记录
                try {
                    val user = User("14","张三","女","zhangsan@example.com")
                    withContext(Dispatchers.IO) {
                        roomUtils.insertUser(user)
                        LogUtils.d(TAG, "添加用户数据$user")
                    }
                    tvAdd.text = user.toString()
                }catch (e:java.lang.Exception){
                    e.stackTrace
                    LogUtils.e(TAG,e.message.toString())
                }
            }
        }

        tvUpdate.setOnClickListener {
            GlobalScope.launch(Dispatchers.Main) {
                // 更新一条用户记录
                val user1 = User("12","欧四","男","ousi@example.com")
                withContext(Dispatchers.IO) {
                    roomUtils.updateUser(user1)
                }
                val user = roomUtils.getAllUsers()
                LogUtils.d(TAG, "===更新用户数据===$user")
                tvUpdate.text = user.toString()
            }
        }

        tvQuery.setOnClickListener {
            GlobalScope.launch(Dispatchers.Main) {
                // 获取所有用户记录
               val user =  withContext(Dispatchers.IO) {
                     roomUtils.getAllUsers()
                }
                LogUtils.d(TAG, "===获取所有用户数据===$user")
                tvQuery.text = user.toString()
            }
        }

        tvDelete.setOnClickListener {
            GlobalScope.launch(Dispatchers.Main) {
                val user = User("12","张三","女","zhangsan@example.com")
                // 删除一条用户记录
                withContext(Dispatchers.IO) {
                    roomUtils.deleteUser(user)
                    LogUtils.d(TAG, "===删除用户数据===$user")
                }
                val user1 = roomUtils.getAllUsers()
                tvDelete.text = user1.toString()
            }
        }

        tvDeleteUsers.setOnClickListener {
            GlobalScope.launch(Dispatchers.Main) {
                val user = roomUtils.getAllUsers()
                val user1 : List<User>
                // 删除所有用户
                withContext(Dispatchers.IO) {
                    roomUtils.deleteUsers(user)
                    user1 = roomUtils.getAllUsers()
                    LogUtils.d(TAG, "===删除多个用户数据===$user1")
                }
                tvDeleteUsers.text = user1.toString()
            }
        }

        tvDeleteAll.setOnClickListener {
            GlobalScope.launch(Dispatchers.Main) {
                val user : List<User>
                // 删除所有用户
                withContext(Dispatchers.IO) {
                    roomUtils.deleteAllUser()
                    user = roomUtils.getAllUsers()
                    LogUtils.d(TAG, "===删除所有用户数据===$user")
                }
                tvDeleteAll.text = user.toString()
            }
        }
    }
}